package com.terry.gametest.test.util;

import org.springframework.expression.EvaluationContext;
import org.springframework.expression.Expression;
import org.springframework.expression.ExpressionParser;
import org.springframework.expression.spel.standard.SpelExpressionParser;
import org.springframework.expression.spel.support.StandardEvaluationContext;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

public class SpelParser {
    private static ExpressionParser parser = new SpelExpressionParser();
    public static String extraKey(String key,String[] parameterNames,Object[] args){
        //第一步key字符串解析为el表达式
        Expression expression = parser.parseExpression(key);
        //第二步 将形参和形参值以配对的方式配置到赋值上下文中
        EvaluationContext context = new StandardEvaluationContext();
        if(args.length<=0){
            return null ;
        }
        for(int i=0;i<args.length;i++){
            context.setVariable(parameterNames[i],args[i]);
        }
        //第三步 赋值给erp
        return expression.getValue(context,String.class);
    }
}
